/*工具类*/
var $, layer;
layui.use([ 'layer', 'jquery' ], function() {
    layer = layui.layer;
    $ = layui.$;
});

var CoreUtil = (function () {
    var coreUtil = {};


    /*存入本地缓存*/
    coreUtil.setData = function(key, value){
        layui.data('LocalData',{
            key :key,
            value: value
        })
    };
    /*从本地缓存拿数据*/
    coreUtil.getData = function(key){
        var localData = layui.data('LocalData');
        return localData[key];
    };

    //判断字符是否为空的方法
    coreUtil.isEmpty = function(obj){
        if(typeof obj == "undefined" || obj == null || obj == ""){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 不刷新当前页
     *
     * @param url
     * @param data
     * @returns
     */
    coreUtil.ajaxPost = function ajaxPost(url, data) {
        coreUtil.ajaxPostLoad(url, data, false);
    }

    /**
     * 全局ajax-post提交(刷新当前页)
     */
    coreUtil.ajaxPostLoad = function ajaxPostLoad(url, data, reload,active) {
        $.ajax({
            cache : true,
            type : "POST",
            url : url,
            data : data,
            // async : false, //同步
            layerIndex : -1,
            beforeSend : function() {
                this.layerIndex = parent.layer.load(0, {
                    shade : [ 0.1, '#000' ]
                });
            },
            complete : function() {
                parent.layer.close(this.layerIndex);
            },
            success : function(data) {
                if (data.code == 200) {
                    parent.layer.msg("操作成功");
                    if (reload != false) {// 刷新
                        active.reload(); // 当前页面刷新
                        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                        parent.layer.close(index);
                    }
                }else if(data.code == 400){
                    parent.layer.msg(data.msg);
                } else {

                    layer.alert(data.msg)
                }
            },
            error : function(request) {
                layer.msg("请检查参数");
            }
        });
    }

    return coreUtil;
})(CoreUtil, window);
